package org.example.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @ Author     ：hanxi
 * @ Date       ：Created in 15:47 2024/3/22
 * @ Description：json null值处理
 */

public class MyStringBeanSerializerModifier extends BeanSerializerModifier {
    private JsonSerializer _nullStringJsonSerializer = new MyNullStringJsonSerializer();
    private JsonSerializer _nullNumberJsonSerializer = new MyNullNumberJsonSerializer();
    private JsonSerializer _nullArrayJsonSerializer = new MyNullArrayJsonSerializer();
    private JsonSerializer _nullBooleanJsonSerializer = new MyNullBooleanJsonSerializer();
    private JsonSerializer _nullDateJsonSerializer = new MyNullDateJsonSerializer();
    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,
                                                     List<BeanPropertyWriter> beanProperties) {
        //循环所有的beanPropertyWriter
        for(int i = 0; i < beanProperties.size(); i++) {
            BeanPropertyWriter writer=beanProperties.get(i);
            //判断字段的类型，如果是array，list，set则注册nullSerializer
            if(isStringType(writer)) {
                //给writer注册一个自己的nullSerializer
                writer.assignNullSerializer(this._nullStringJsonSerializer);
            }else if(isNumberType(writer)){
                writer.assignNullSerializer(this._nullNumberJsonSerializer);
            }else if(isArrayType(writer)){
                writer.assignNullSerializer(this._nullArrayJsonSerializer);
            }else if(isBooType(writer)){
                writer.assignNullSerializer(this._nullBooleanJsonSerializer);
            }else if(isDateType(writer)){
                writer.assignNullSerializer(_nullDateJsonSerializer);
            }
        }
        return beanProperties;
    }
    //判断是什么类型
    private boolean isStringType(BeanPropertyWriter writer) {
        JavaType clazz =writer.getType();
        Class c = clazz.getRawClass();
        return c.equals(String.class);
    }
    //判断是什么类型
    private boolean isNumberType(BeanPropertyWriter writer) {

        JavaType clazz =writer.getType();
        Class c = clazz.getRawClass();
        return c.equals(Double.class) || c.equals(Integer.class) || c.equals(Long.class)|| c.equals(Short.class)|| c.equals(Byte.class)|| c.equals(BigDecimal.class);
    }
    //判断是什么类型
    private boolean isArrayType(BeanPropertyWriter writer) {

        JavaType clazz =writer.getType();
        Class c = clazz.getRawClass();
        return c.isArray() || Collection.class.isAssignableFrom(c);
    }
    //判断是什么类型
    private boolean isBooType(BeanPropertyWriter writer) {

        JavaType clazz =writer.getType();
        Class c = clazz.getRawClass();
        return c.equals(boolean.class)|| c.equals(Boolean.class);
    }
    //判断是什么类型
    private boolean isDateType(BeanPropertyWriter writer) {

        JavaType clazz =writer.getType();
        Class c = clazz.getRawClass();
        return c.equals(Date.class);
    }
}

class MyNullStringJsonSerializer extends JsonSerializer {
    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)throws IOException, JsonProcessingException {
        if(value == null) {
            jgen.writeString("");
        }else{
            jgen.writeObject(value);
        }
    }
}

class MyNullArrayJsonSerializer  extends JsonSerializer {
    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)throws IOException, JsonProcessingException {
        if(value ==null) {
            jgen.writeStartArray();

            jgen.writeEndArray();
        }else{
            jgen.writeObject(value);
        }
    }
}

class MyNullBooleanJsonSerializer  extends JsonSerializer {
    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)throws IOException, JsonProcessingException {
        if(value ==null) {
            jgen.writeBoolean(false);
        }else{
            jgen.writeObject(value);
        }
    }
}

class MyNullDateJsonSerializer extends JsonSerializer {
    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)throws IOException, JsonProcessingException {

        jgen.writeNumber(0);

    }
}

class MyNullNumberJsonSerializer extends JsonSerializer {
    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)throws IOException, JsonProcessingException {

        jgen.writeNumber(0);

    }
}
